﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Common.Logging;
using InfoboardAdapterC.Properties;
using NKnife.IoC;

namespace InfoboardAdapterC.Connection
{
    public partial class ConnectionView : Form
    {
        private static readonly ILog _loggerLog = LogManager.GetLogger<ConnectionView>();
        private readonly DbConnectionViewModel _DbConnectionView = DI.Get<DbConnectionViewModel>();
        public ConnectionView()
        {
            InitializeComponent();
            BindingConfig();
        }

        private void BindingConfig()
        {
            _DbConnectionView.LoadConfig(out string dbServerName, out string dbUserName,
                out string dbPassword,out string dbName);
            ServerNameComboBox.Text = dbServerName;
            UserNameTextBox.Text = dbUserName;
            UserPasswordTextBox.Text = dbPassword;
            DbNameComboBox.Text = dbName;
        }

        /// <summary>
        /// 绑定服务器集合
        /// </summary>
        private void BindServerNames()
        {
            try
            {
                var serverNames = _DbConnectionView.LoadServerNames();
                if (serverNames.Count <= 0) return;
                BindComboxList(serverNames, ServerNameComboBox);
            }
            catch (Exception exception)
            {
                _loggerLog.Error(exception.Message);
            }
           
        }
        /// <summary>
        /// 绑定数据库集合
        /// </summary>
        private void BindDbNames()
        {
            try
            {
                if (ServerNameComboBox.Text.Length <= 0) return;
                if (UserNameTextBox.Text.Length <= 0) return;
                if (UserPasswordTextBox.Text.Length <= 0) return;
                var dbnames = _DbConnectionView.LoadDbNames(ServerNameComboBox.Text, UserNameTextBox.Text,
                    UserPasswordTextBox.Text);
                if (dbnames.Count <= 0) return;
                BindComboxList(dbnames, DbNameComboBox);
            }
            catch (Exception exception)
            {
                _loggerLog.Error(exception.Message);
            }
          
        }

        /// <summary>
        /// 为Combox添加集合
        /// </summary>
        /// <param name="list"></param>
        /// <param name="comboBox"></param>
        private static void BindComboxList(IEnumerable<string> list, ComboBox comboBox)
        {
            comboBox.Items.Clear();
            foreach (var name in list)
            {
                comboBox.Items.Add(name);
            }
        }

        private void DbNameComboBox_Click(object sender, EventArgs e)
        {
            try
            {
                BindDbNames();
            }
            catch (Exception exception)
            {
                _loggerLog.Error(exception.Message);
            }
        }

        private void ServerNameComboBox_DropDown(object sender, EventArgs e)
        {
            BindServerNames();
        }

        private void TestConnecitonButton_Click(object sender, EventArgs e)
        {
            try
            {
                var showText = "测试连接失败";
                if (_DbConnectionView.TestConnection(ServerNameComboBox.Text, UserNameTextBox.Text,
                    UserPasswordTextBox.Text))
                    showText = "测试连接成功";
                MessageBox.Show(showText);
            }
            catch (Exception exception)
            {
                _loggerLog.Error(exception.Message);
            }
            
        }

        private void SaveConfigButton_Click(object sender, EventArgs e)
        {
            var reslut = "保存失败,原因:";
            try
            {
                _DbConnectionView.SaveConfig(ServerNameComboBox.Text, UserNameTextBox.Text,
                    UserPasswordTextBox.Text,DbNameComboBox.Text);
                reslut = "保存完成";
            }
            catch (Exception exception)
            {
                reslut += exception.Message;
                _loggerLog.Error(exception.Message);
            }
           MessageBox.Show(reslut);
        }
    }
}
